<?php

require_once "TcAI.Config.php";

class TcAITool
{
    public static function doHttpPost($url, $params)
    {
        // 开启curl
        $curl = curl_init();

        $response = false;
        do {
            // 1. 设置HTTP URL (API地址)
            curl_setopt($curl, CURLOPT_URL, $url);
            // 2. 设置HTTP HEADER (表单POST)
            $head = array(
                'Content-Type: application/x-www-form-urlencoded',
            );
            curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
            // 3. 设置HTTP BODY (URL键值对)
            $body = http_build_query($params);
            curl_setopt($curl, CURLOPT_POST, true);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
            // 4. 调用API，获取响应结果
            curl_setopt($curl, CURLOPT_HEADER, false);
            curl_setopt($curl, CURLOPT_NOBODY, false);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
            // ssl https放开下面代码
            // curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            $response = curl_exec($curl);
            if ($response === false) {
                //记录错误
                self::log('Curl error: ' . curl_error($curl));
                $response = false;
                break;
            }

            $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
            if ($code != 200) {
                //记录错误
                self::log('Curl error: ' . curl_error($curl));
                $response = false;
                break;
            }
        } while (0);
        // 关闭资源
        curl_close($curl);
        return $response;
    }

    public static function getReqSign($params)
    {
        // 1. 字典升序排序
        ksort($params);
        // 2. 拼按URL键值对
        $str = '';
        foreach ($params as $key => $value) {
            if ($value !== '') {
                $str .= $key . '=' . urlencode($value) . '&';
            }
        }
        // 3. 拼接app_key
        $str .= 'app_key=' . TcAIConfig::APPKEY;
        // 4. MD5运算+转换大写，得到请求签名
        $sign = strtoupper(md5($str));
        return $sign;
    }

    public static function p($data)
    {
        echo '<pre>';
        print_r($data);
        echo '</pre>';
    }

    public static function base64EncodeImage($path)
    {
        $data = file_get_contents($path);
        $base64 = base64_encode($data);
        return $base64;
    }

    public static function showBase64($string)
    {
        return 'data:image/jpeg;base64,' . $string;
    }

    public static function log($log)
    {
        //自定义log目录位置和日志文件名字
        $path = './log/' . date('y_m_d') . '.log';
        // 自动创建日志目录
        $log_dir = dirname($path);
        if (!is_dir($log_dir)) {
            mkdir($log_dir, 0755, true);
        }
        //检测日志文件大小，超过配置大小则备份日志文件重新生成
        if (is_file($path) && 2097152 <= filesize($path)) {
            rename($path, dirname($path) . '/' . time() . '-' . basename($path));
        }
        //日志格式
        $log = print_r($log, true);
        $now = date('Y-m-d H:i:s');
        error_log("[{$now}] " . $_SERVER['REMOTE_ADDR'] . ' ' . $_SERVER['REQUEST_URI'] . "\r\n{$log}\r\n", 3, $path);

    }

}
